const { userRegisterError } = require("../constant/err.type");
const bcrypt = require("bcryptjs")
const {
  addUser,
  getGiteeService,
  setUserInfo,
  getGiteeUserInfo,
  queryUserListServie,
  queryRoleListService,
  getUserAccountInfo,
  getUserMessage,
  modifyUserByIdService,
  modifyPwdService
} = require("../service/user.service.js");
const jwt = require("jsonwebtoken");
const { TOKEN_KEY } = require("../config/default");
const { JsonWebTokenError } = require("../constant/err.type");

const GiteeClientId =
  "32813b2da3a3165de8eca86b77fa07076f1f274cb758d60b80583b56e657dd12";
const RedirectUrl = "http://localhost/bl-user/callback";

class UserController {
  async register(ctx, next) {
    console.log(ctx.request.body, "--len");
    try {
      await addUser(ctx.request.body);
      ctx.body = {
        message: "success",
        code: "200",
      };
    } catch (error) {
      ctx.app.emit("error", userRegisterError, ctx);
    }
  }
  // 登录成功返回token
  async login(ctx, next) {
    console.log(ctx.user, "controller-ctx.user");
    const { password, ...data } = ctx.user;
    ctx.status = 200;
    ctx.body = {
      token: jwt.sign(data, TOKEN_KEY, { expiresIn: "1d" }),
      message: "登录成功",
      result: null,
    };
  }

  async getUserInfo(ctx, next) {
    const { username } = ctx.user;
    const res = await getUserMessage({ username });

    ctx.status = 200;
    ctx.body = {
      message: "获取用户信息成功",
      result: { ...res[0], password: "" },
      code: "200",
    };
  }
  // gitee
  async handleLoginControl(ctx, next) {
    console.log("gitee");
    let url = `https://gitee.com/oauth/authorize?client_id=${GiteeClientId}&redirect_uri=${RedirectUrl}&response_type=code`;
    ctx.redirect(url);
  }
  // weibo
  async handleWeiboLoginControl(ctx, next) {}
  async handleCallback(ctx, next) {
    // 授权成功拿到code
    const code = ctx.query.code;
    console.log(code, "code---code");
    const token = await getGiteeService(code);
    console.log(token, "拿到的数据");
    ctx.redirect(`http://localhost:5173/#/?token=${token}`);
  }

  // 第三方登录
  async handleGetUserInfo(ctx, next) {
    const { authorization } = ctx.request.header;
    console.log("token", authorization);
    try {
      const res = await getGiteeUserInfo(authorization);
      console.log("第三方登录token", res);
      const userinfo = await setUserInfo(res.data);
      console.log(userinfo, "giteeinfo");
      ctx.body = {
        code: 200,
        message: "获取用户信息成功！",
        data: userinfo,
      };
    } catch (error) {
      ctx.app.emit("error", JsonWebTokenError, ctx);
    }
  }

  // 账号获取信息
  async getUserInfoControl(ctx, next) {
    const { authorization } = ctx.request.header;
    console.log("token", authorization);
    try {
      const userinfo = await getUserAccountInfo(authorization);
      console.log(userinfo, "accountinfo");
      ctx.body = {
        code: 200,
        message: "获取用户信息成功！",
        data: userinfo,
      };
    } catch (error) {
      ctx.app.emit("error", JsonWebTokenError, ctx);
    }
  }

  async queryUserListControl(ctx, next) {
    const requestObj = ctx.request.body || {};
    const res = await queryUserListServie(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }

  async queryRoleListControl(ctx, next) {
    const requestObj = ctx.request.body || {};
    const res = await queryRoleListService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }
  async modifyUserById(ctx, next) {
    const requestObj = ctx.request.body;
    console.log("modify", requestObj);
    const res = await modifyUserByIdService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }

  // 修改密码
  async modifyPwd(ctx, next) {
    const requestObj = ctx.request.body;
    const salt = bcrypt.genSaltSync(10);
    const hash = bcrypt.hashSync(requestObj.newPwd, salt);
    requestObj.newPwd = hash;
    console.log("newPwd",requestObj);
    const res = await modifyPwdService(requestObj);
    ctx.body = {
      code:200,
      message:"success",
      data:null,
    }
  }
}
module.exports = new UserController();
